#include <stdio.h>
#include <SDL2/SDL.h>

#define SDL_DELAY 12

SDL_Window *window;
SDL_Renderer *render;
SDL_Event e;
int gQuit = 0;

int init()
{
    int ret = 1;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("sdl could not init!sdl error:%s\n", SDL_GetError());
        ret = 0;
    }
    else
    {
        if ((window = SDL_CreateWindow("sdl", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 300, 0)) == NULL)
        {
            printf("could not create! error:%s\n", SDL_GetError());
            ret = 0;
        }
        else
        {
            if ((render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)) == NULL)
            {
                printf("render could not create! error:%s\n", SDL_GetError());
                ret = 0;
            }
        }
    }
    return ret;
}
void handleButtons()
{
    switch (e.type)
    {
    case SDL_QUIT:
        gQuit = 1;
        break;
    case SDL_KEYDOWN:
        if (e.key.keysym.sym == 'q')
            gQuit = 1;
        break;
    }
}
void tick()
{
    SDL_RenderClear(render);

    while (SDL_PollEvent(&e) != 0)
    {
        handleButtons();
    }
    SDL_SetRenderDrawColor(render, 0, 0, 255, 255);
    SDL_RenderPresent(render);
}

void quit()
{
    SDL_DestroyRenderer(render);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

void loop()
{
    while (!gQuit)
    {
        SDL_Delay(SDL_DELAY);
        tick();
    }
    quit();
}
int main(int argc, char *argv[])
{
    if (!init())
    {
        printf("init error!\n");
    }
    else
    {
        loop();
    }
    return 0;
}